# Dreams have to come true after all
from flask import Flask
from settings import map_config


# 负责创建项目
def create_app(config_type):
    app = Flask(__name__)
    # 加载项目的配置
    app.config.from_object(map_config.get(config_type))
    # 初始化限流器
    from comment.util.limiter import limiter as lmt
    lmt.init_app(app)
    # 加载日志处理工具
    from comment.util.log import create_logger
    create_logger(app)
    # 初始化sqlalchemy
    from comment.models import db
    db.init_app(app)
    # 加载蓝图
    from Shopping.resource.user import user_bp
    app.register_blueprint(user_bp)
    from Shopping.resource.index import index_bp
    app.register_blueprint(index_bp)
    from Shopping.resource.goods import goods_bp
    app.register_blueprint(goods_bp)
    from Shopping.resource.coupon import coupon_bp
    app.register_blueprint(coupon_bp)
    from Shopping.resource.cart import cart_bp
    app.register_blueprint(cart_bp)
    from Shopping.resource.address import address_bp
    app.register_blueprint(address_bp)
    # 初始化redis数据库的连接
    from comment.util.shopping_redis import redis_client
    redis_client.init_app(app)
    # 添加请求钩子
    from comment.util.requests_wares import jwt_request_authorization
    app.before_request(jwt_request_authorization)  # 在所有请求中都生效
    return app
